package com.own.business.log.operation.persistence.controller.manage;

import com.own.aop.permission.PermissionModule;
import com.own.business.log.operation.persistence.business.service.LogOperationElasticService;
import com.own.business.log.operation.persistence.common.constant.ConstantModule;
import com.own.business.log.operation.persistence.entity.query.LogOperationStatsTimeRangeQuery;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.ItemCountModel;
import com.own.component.common.model.ItemLongCountModel;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.common.model.entity.ChildrenItemModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * ManageLogOperationPersistenceStatsController
 *
 * @author chenxueli
 * @date 2024-05-08 03:39:51
 */
@Tag(name = "操作日志持久化stats" + ConstantBeanSuffix.MANAGE_CLIENT + ConstantBeanSuffix.CONTROLLER)
@RestController
@PermissionModule(ConstantModule.LOG_OPERATION_MANAGE)
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + ConstantApiPrefix.MANAGE_CLIENT + "log/operation/stats")
public class ManageLogOperationPersistenceStatsController extends BaseController {

    @Resource
    private LogOperationElasticService logOperationElasticService;

    @LogOperationAop
    @Operation(summary = "根据时间信息查询统计数据")
    @PostMapping("/time/range")
    public ResultModel<List<ChildrenItemModel<ItemCountModel, ItemCountModel>>> statsForTimeRange(
            @RequestBody LogOperationStatsTimeRangeQuery query
    ) {
        var list = logOperationElasticService.statsForTimeRange(query);
        return R.success(list);
    }

    @LogOperationAop
    @Operation(summary = "统计时间范围内的用户id操作次数")
    @PostMapping("/user/time/range")
    public ResultModel<List<ChildrenItemModel<ItemCountModel, ItemLongCountModel>>> statsForUserTimeRange(
            @RequestBody LogOperationStatsTimeRangeQuery query
    ) {
        var list = logOperationElasticService.statsForUserTimeRange(query);
        return R.success(list);
    }

    @LogOperationAop
    @Operation(summary = "统计时间范围内的用户id数量")
    @PostMapping("/user/count/time/range")
    public ResultModel<List<ItemCountModel>> statsForUserCountTimeRange(
            @RequestBody LogOperationStatsTimeRangeQuery query
    ) {
        var list = logOperationElasticService.statsForUserCountTimeRange(query);
        return R.success(list);
    }

    @LogOperationAop
    @Operation(summary = "统计时间范围内的用户id数量（仅app端）")
    @PostMapping("/only/app/user/count/time/range")
    public ResultModel<List<ItemCountModel>> statsForUserCountTimeRangeOnlyApp(
            @RequestBody LogOperationStatsTimeRangeQuery query
    ) {
        query.setOnlyApp(1);
        var list = logOperationElasticService.statsForUserCountTimeRange(query);
        return R.success(list);
    }

    @LogOperationAop
    @Operation(summary = "统计时间范围内的设备数量")
    @PostMapping("/device/count/time/range")
    public ResultModel<List<ChildrenItemModel<ItemCountModel, ItemCountModel>>> statsForDeviceCountTimeRange(
            @RequestBody LogOperationStatsTimeRangeQuery query
    ) {
        var list = logOperationElasticService.statsForDeviceCountTimeRange(query);
        return R.success(list);
    }

}
